<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.username" placeholder="员工名"></el-input>
                </el-form-item>

                <el-select v-model="filters.state" placeholder="请选择状态">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="在职" value="1"></el-option>
                    <el-option label="离职" value="0"></el-option>
                </el-select>

                <el-form-item>
                    <el-button type="primary" v-on:click="getSuperEmployee">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="employee" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="排序" width="80">
            </el-table-column>
            <el-table-column prop="id" label="编号" width="120" sortable>
            </el-table-column>
            <el-table-column prop="username" label="姓名" width="120" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="120" sortable>
            </el-table-column>
            <el-table-column prop="phone" label="电话" width="140" sortable>
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="120" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100" :formatter="formatState"  sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <!--新增修改框-->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" >
            <el-form  :model="addform" :rules="addformrules" class="demo-form-inline" ref="addform">
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="addform.username" placeholder="姓名"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addform.email" placeholder="邮箱"></el-input>
                </el-form-item>

                <el-form-item label="电话" prop="phone">
                    <el-input v-model="addform.phone" placeholder="电话"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="addform.password" placeholder="密码" ></el-input>
                </el-form-item>

                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="addform.checkPass" ></el-input>
                </el-form-item>

                <el-form-item label="年龄" prop="age">
                    <el-input v-model.number="addform.age" placeholder="年龄"></el-input>
                </el-form-item>


                <el-form-item label="状态" >
                    <el-radio-group v-model="addform.state" >
                        <el-radio  value="在职" :label=1>在职</el-radio>
                        <el-radio  value="离职" :label=0>离职</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary"@click.native="addSubmit" :loading="addLoading">确 定</el-button>
            </span>
        </el-dialog>



    </section>
</template>

<script>

    export default {
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    console.log(reg.test(value));
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };

            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.addform.checkPass !== '') {
                        this.$refs.addform.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.addform.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                filters: {
                    username: '',
                    state:""
                },
                employee: [],
                total: 0,
                pageSize: 10,
                currentPage:1,
                listLoading: false,
                sels: [],//列表选中列
                //默认隐藏添加与修改框
                dialogVisible : false,
                addform :{
                    username: '',
                    email: '',
                    phone: '',
                    password:"",
                    age:"",
                    state:"",
                    //比较密码
                    checkPass:""
                },
                title:"",
                //添加和修改的加载框
                addLoading: false,

                addformrules: {
                    username: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                    ],
                    password: [
                        {  validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        {   validator: validatePass2, trigger: 'blur' }
                    ],
                    phone: [
                        {  validator: checkPhone, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ]

                }




            }
        },
        methods: {
            //状态显示转换
            formatState: function (row, column) {
                return row.state == 1 ? '在职' : '离职';
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getEmployee();
            },
            //获取部门列表
            getEmployee() {
                this.listLoading = true;  //开启加载圈

                let param = {"currentPage": this.currentPage,
                    "pageSize": this.pageSize,
                    "username":this.filters.username,
                    "state":this.filters.state};
                this.$http.patch("/employee",param).then((res=>{
                    //console.debug(res)
                    this.employee = res.data.result
                    this.total = res.data.total
                }))
                this.listLoading = false;  //关闭加载圈
            },
            //删除
            handleDel: function (index, row) {
                //console.debug(row)
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //准备参数 传入id
                    let param = {"id":row.id};
                    //发送axios请求
                    this.$http.delete("/employee/"+row.id,param).then((result=>{


                        //console.debug(result.data.success)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: result.data.msg
                            });
                        }else {
                            this.$message.error(result.data.msg);
                        }
                        //删除后刷新列表
                        this.getEmployee()
                    }));
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //显示编辑界面
            handleEdit: function (index, row) {
                console.debug(row)
                //回显数据
                for(var k in row){
                    this.addform[k] = row[k];
                }
                //回显确认密码框使其等于密码
                this.addform.checkPass = this.addform.password;
                //显示对话框
                this.title = "请修改"
                this.dialogVisible = true;
            },
            //显示新增界面
            handleAdd: function () {
                //清空表单
                /*this.addform = {
                    username: '',
                    email: '',
                    phone: '',
                    password:"",
                    age:"",
                    state:""
                }*/
                for(var k in this.addform){
                    this.addform[k] = "";
                }
                //默认启用
                this.addform.state = 1;
                this.title = "请添加"
                //弹出框
                this.dialogVisible = true;
            },

            //新增
            addSubmit: function () {
                this.$refs.addform.validate((valid) => {
                    if (valid) {//是否通过验证
                        let param = Object.assign({}, this.addform);
                        if(param.id){//id不为空就是修改
                            this.$http.post("/employee",param).then((result=>{
                                if(result.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: result.data.msg
                                    });
                                }else {
                                    this.$message.error(result.data.msg);
                                }
                                //关闭修改\框
                                this.dialogVisible = false;
                                //修改后刷新列表
                                this.getEmployee()
                            }))
                        }else {//id为空就是新增
                            //console.debug(param)
                            this.$http.put("/employee",param).then((result=>{

                                if(result.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: result.data.msg
                                    });
                                }else {
                                    this.$message.error(result.data.msg);
                                }
                                //关闭新增框
                                this.dialogVisible = false;
                                //新增后刷新列表
                                this.getEmployee()
                            }))
                        }

                    } else {
                        return false;
                    }
                });

            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {

                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //获取选中行的id，最终以数组展示
                    var ids = this.sels.map(item => item.id);//[1,2,3,4]
                    //发送axios请求
                    this.$http.patch("/employee/batchRemove",ids).then((result=>{
                        //console.debug(result.data.success)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: result.data.msg
                            });
                        }else {
                            this.$message.error(result.data.msg);
                        }
                        //删除后刷新列表
                        this.getEmployee()
                    }));
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //高级查询
            getSuperEmployee(){
                let param = Object.assign({}, this.filters);
                //console.debug(param)
                this.getEmployee()
            }


        },
        mounted() {
            this.getEmployee()

        }
    }
</script>

